<template>
    <div>
        <h1>会员添加</h1>
        <table class="table table-bordered" >
    <tbody>
        <tr align="right">
            <td>帐号</td>
            <td>
                <input type="text" v-model="state.name"/>
            </td>
        </tr>

        <tr align="right">
            <td>密码</td>
            <td>
                <input type="password" v-model="state.pwd" />
            </td>
        </tr>

        <tr align="right">
            <td>确认密码</td>
            <td>
                <input type="text" v-model="Pwd1" />
            </td>
        </tr>

        <tr align="right">
            <td>手机号</td>
            <td>
                <input type="text" v-model="state.phone" maxlength="11" />
            </td>
        </tr>

        <tr align="right">
            <td>身份证号</td>
            <td>
                <input type="text" v-model="state.card" maxlength="18" />
            </td>
        </tr>

        <tr align="right">
            <td>性别</td>
            <td>
                <input type="radio" name="sex"  v-model="state.sex" :value="false" />男
                <input type="radio" name="sex"  v-model="state.sex" :value="true" />女
            </td>
        </tr>
        <tr align="center">
           
            <td>
                <input type="button" value="添加" @click="add"/>
            </td>
        </tr>
    </tbody>
</table>
    </div>
</template>

<script setup lang="ts">
import { reactive, toRefs} from "vue";
import axios from "axios";
import { useRouter } from "vue-router";

const router = useRouter();
let data:any=reactive({
    state:{       
        "name": '',
  "pwd": '',
  "card": '',
  "phone": '',
  sex: true,

    },

      Pwd1:"",
});
let {state,Pwd1} = toRefs(data);

const add=()=>{
 if(state.value.name==""){
        alert("帐号不能为空");
        return;
    }
    if(state.value.pwd==""){
        alert("密码不能为空");
        return;
    }
    if(state.value.phone==""){
        alert("手机号不为能空");
        return;
    }
    if(state.value.card==""){
        alert("身份证号不能为空");
        return;
    }
    let reg=/^1[3-9]\d{9}$/;
    if(!reg.test(state.value.phone)){
        alert("手机号格式不正确");
        return;
    }
    let reg1=/^\d{17}(\d|X)$/i;
    if(!reg1.test(state.value.card)){
        alert("身份证号格式不正确");
        return;
    }
    if(state.value.pwd!=Pwd1.value){
        alert("两次密码不一致");
        return;
    }
   

    let obj={
        name:state.value.name,
        pwd:state.value.pwd,
        card:state.value.card,
        phone:state.value.phone,
        Sex:state.value.Sex,
    };
    axios({
        url:'/api/Member/AddMember/AddMember',
        method:'post',
        data:obj,
        
    })
    .then((res)=>{
        if(res.data.data==1){
            alert("添加成功");
            router.push('/membershow')  
        }
        else if(res.data.data==-1){
            alert('电话号码重复');
            return;
        }
        else{
            alert("添加失败");
        }
    })
    
    
}


</script>

<style scoped>

</style>